package com.fxw.mylibrary.util;

import android.graphics.Paint;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.text.style.RelativeSizeSpan;
import android.widget.TextView;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Title:string工具类
 */
public class StringUtils {


    /**
     * 获取string的长度
     *
     * @param text
     * @return
     * @Description:
     */
    public static int getStringLength(String text) {
        text = text.replaceAll("[^\\x00-\\xff]", "**");
        return text.length();
    }

    /**
     * 检查字符串是否包含电话号码
     *
     * @param text
     * @return
     */
    public static boolean checkContainPhone(String text) {
        Pattern p = Pattern.compile("[0-9]");
        Matcher m = p.matcher(text);
        boolean result = false;
        StringBuffer mBuffer = new StringBuffer();
        while (m.find()) {
            mBuffer.append(m.group());
            result = checkPhone(mBuffer.toString());
        }
        return result;
    }

    /**
     * 验证手机号码是否合格
     *
     * @return
     * @Description:
     */
    public static boolean checkPhone(String phone) {
        String regex = "^1[3|4|5|7|8|][0-9]{9}$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(phone);
        return matcher.matches();
    }

    /**
     * 验证邮箱格式是否合格
     *
     * @param email
     * @return
     * @Description:
     */
    public static boolean checkEmail(String email) {
        String regex = "^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$";
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(email);
        return matcher.matches();
    }

    /**
     * 验证密码长度6-16位
     *
     * @param password
     * @return
     * @Description:
     */
    public static boolean checkPasswordLength(String password) {
        int length = password.length();
        if (length > 16 || length < 6) {
            return false;
        }
        return true;
    }

    /**
     * 添加删除线
     *
     * @param v
     */
    public static void addStrikeThrough(TextView v) {
        v.getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
    }


    /**
     * 改变字体颜色
     *
     * @param tv    tv控件
     * @param color 颜色值
     * @param start 改变颜色的起始位置
     * @param end   结束位置
     */
    public static void setdiffrentColor(TextView tv, int color, int start, int end) {
        SpannableStringBuilder style = new SpannableStringBuilder(tv.getText());
        ForegroundColorSpan redSpan = new ForegroundColorSpan(color);
        style.setSpan(redSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(style);
    }

    /**
     * 改变 字体 颜色 大小
     *
     * @param tv    tv控件
     * @param color 颜色值
     * @param size  字体大小值
     * @param start 改变颜色的起始位置
     * @param end   结束位置
     * @return
     */
    public static void setdiffrentColor(TextView tv, int color, int size, int start, int end) {
        SpannableStringBuilder style = new SpannableStringBuilder(tv.getText());
        ForegroundColorSpan redSpan = new ForegroundColorSpan(color);
        style.setSpan(redSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        RelativeSizeSpan sizeSpan = new RelativeSizeSpan(size);
        style.setSpan(sizeSpan, start, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv.setText(style);
    }

    /**
     * 排序拼接字符串
     *
     * @param params
     * @return
     */
    public static String getSignStr(Map<String, String> params) {
        List<String> keys = new ArrayList<String>(params.keySet());
        Collections.sort(keys);
        String prestr = "";
        for (int i = 0; i < keys.size(); i++) {
            String key = keys.get(i);
            String value = params.get(key);
            if (i == keys.size() - 1) {//拼接时，不包括最后一个&字符
                prestr = prestr + key + "=" + value;
            } else {
                prestr = prestr + key + "=" + value + "&";
            }
        }
        return prestr;
    }


    /**
     * sha1加密
     *
     * @param params
     * @param key
     * @return
     */
    public static String getSha1(Map<String, String> params, String key) {
        String str = getSignStr(params) + key;
        if (str == null || str.length() == 0) {
            return null;
        }
        byte[] digesta = null;
        try {
             // 得到一个SHA-1的消息摘要
            MessageDigest alga = MessageDigest.getInstance("SHA-1");
             // 添加要进行计算摘要的信息
            alga.update(str.getBytes());
            // 得到该摘要
            digesta = alga.digest();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        // 将摘要转为字符串
        String rs = byte2hex(digesta);
        return rs;
    }

    public static String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";
        for (int n = 0; n < b.length; n++) {
            stmp = (Integer.toHexString(b[n] & 0XFF));
            if (stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }
        return hs;
    }
}
